﻿@using System.Data;
@using GatewayServer.Common;
@using UtilsBase;
@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {

        if ("@(ViewBag.order)" == "upd") {
            getEdit("@(ViewBag.guid)");
        }
        else {
            getDeviceList();

        }



    });


    function getEdit(dguid) {
        $.post("getModel", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            $("#tf_title").val(myobj.tf_title);
            $("#hid_tf_key").val(myobj.tf_key);
            $("#hid_tf_task_guid").val(myobj.tf_task_guid);
            $("#tf_time_value").val(myobj.tf_time_value);

            $("#tf_interval_day").val(myobj.tf_interval_day);
            // $("#tf_repeat_count").val(myobj.tf_repeat_count);
            for (var i = 0; i < document.getElementById("tf_status").options.length; i++) {
                if (document.getElementById("tf_status").options[i].value == myobj.tf_status) {
                    document.getElementById("tf_status").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("tf_status").options[i].selected = false;

                }
            }


            getDeviceList();
            debugger
            getCommandList(myobj.tf_task_guid);
        });
    }

    function getDeviceList() {
        $("#tf_task_guid").empty();
        $("#tf_task_guid").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getDeviceList", { "flag": Math.random(), p_guid: $("#tf_p_guid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#tf_task_guid").prepend("<option value='" + myobj[i].d_guid + "'>" + myobj[i].d_code + "-" + myobj[i].d_name + "</option>")
            }


            for (var i = 0; i < document.getElementById("tf_task_guid").options.length; i++) {
                if (document.getElementById("tf_task_guid").options[i].value == $("#hid_tf_task_guid").val()) {
                    document.getElementById("tf_task_guid").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("tf_task_guid").options[i].selected = false;

                }
            }
        });
    }

    function getCommandList(guid) {
        $("#tf_key").empty();
        $("#tf_key").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getCommandList", { "flag": Math.random(), guid: guid }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#tf_key").prepend("<option value='" + myobj[i].dc_key + "'>" + myobj[i].dc_name + "</option>")
            }

            for (var i = 0; i < document.getElementById("tf_key").options.length; i++) {
                if (document.getElementById("tf_key").options[i].value == $("#hid_tf_key").val()) {
                    document.getElementById("tf_key").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("tf_key").options[i].selected = false;

                }
            }
        });
    }

    function saveAll() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveAll", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                window.location.href = "list?p_guid=" + $("#tf_p_guid").val();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }
</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label {
        width: 150px;
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <input type="hidden" name="tf_task_type" id="tf_task_type" value="device" />
        <input type="hidden" name="tf_p_guid" id="tf_p_guid" value="@ViewBag.tf_p_guid" />
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>定指时间-设备</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>标题：</label>
                            <div class="kv-item-content">
                                <input type="text" name="tf_title" id="tf_title" placeholder="标题" />
                                <input type="hidden" name="tf_guid" id="tf_guid" value="@ViewBag.guid" />
                            </div>
                        </div>

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>状态：</label>
                            <div class="kv-item-content">
                                <select id="tf_status" name="tf_status" style="width:130px;">
                                    <option value="0">启用</option>
                                    <option value="1">禁用</option>
                                </select>

                            </div>
                        </div>

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>设备：</label>
                            <div class="kv-item-content">
                                <select id="tf_task_guid" name="tf_task_guid" style="width:130px;" onchange="getCommandList(this.value)">
                                </select>
                                <input type="hidden" name="hid_tf_task_guid" id="hid_tf_task_guid" value="" />
                            </div>
                        </div>

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>指令：</label>
                            <div class="kv-item-content">
                                <select id="tf_key" name="tf_key" style="width:130px;">
                                </select>
                                <input type="hidden" name="hid_tf_key" id="hid_tf_key" value="" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>执行时间：</label>
                            <div class="kv-item-content">
                                <input type="text" name="tf_time_value" id="tf_time_value" placeholder="时间" />
                                如：22:10
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo"></span>每：</label>
                            <div class="kv-item-content">
                                <input type="text" name="tf_interval_day" id="tf_interval_day" placeholder="每N天执行一次"  value="0"/>
                               天执行一次（0-100： 0当天到点执行，不判断当天是否执行过）
                            </div>
                        </div>
                        @*   <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>执行次数：</label>
                        <div class="kv-item-content">
                        <input type="text" name="tf_repeat_count" id="tf_repeat_count" value="0" placeholder="执行次数" />
                        0无限次，或1-N次
                        </div>
                        </div>*@


                        <div><hr /></div>
                        <div style="margin:0 auto;width:150px;">

                            <input type="button" id="save" name="save" value="保存" onclick="javascript:saveAll()" />
                            &nbsp;&nbsp; <input type="button" id="but_back" name="but_back" value="返回" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

